package com.sk.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sk.dao.GoodDao;
import com.sk.dao.ShopCartDao;
import com.sk.dao.UserDao;
import com.sk.domain.Shopcart;
import com.sk.domain.User;

@Repository("shopcartService")
public class ShopCartService {
	@Autowired
	private ShopCartDao scd;
	@Autowired
	private UserDao ud;
	@Autowired
	private GoodDao gd;

	public boolean addShopCart(User user,int gid) {
		// TODO Auto-generated method stub
		
		Shopcart sc = new Shopcart();
		sc.setAddtime(new Timestamp(new Date().getTime()));
		try {
			sc.setGood(gd.findById(gid));
			sc.setUser(user);
			scd.add(sc);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
		return true;
		
	}
	
	public List<Shopcart> findAll(User user){
		List<Shopcart> scs = null;
		try {
			Shopcart sc = new Shopcart();
			sc.setUser(user);
			scs = (List<Shopcart>) scd.findForList(sc, null).getResultList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
		return scs;
	}

	public boolean  delByID(int id) {
		// TODO Auto-generated method stub
		
		try {
			scd.deleteById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
